var path=require('path')

var wx=require('./index.js')
var wechatApi=wx.getWechat()

exports.reply=function* (next){

   var message=this.weixin

   if (message.MsgType==='event') {
       if (message.Event==='subscribe') {
            if (message.EventKey) {
                console.log('扫二维码进来'+ message.EventKey+' '+ message.ticket)
            }
            this.body='哈哈，你订阅了公众号\r\n'+'消息ID'+message.MsgId+'\r\n'+'输入 1 2 3 4 自动回复不同内容'
       }else if(message.Event==='unsubscribe'){
            console.log('无情取关')
            this.body=''
       }else if(message.Event==='LOCATION'){
        
            this.body='您上报的地理位置是：'+message.Location_X+'/'+message.Location_Y
       }else if(message.Event==='CLICK'){
            this.body='您点击了菜单：'+message.EventKey
       }else if(message.Event==='SCAN'){
            console.log('关注后扫二维码'+message.EventKey+' '+message.Ticket)
            this.body='看到你扫一下哦'
       }else if(message.Event==='VIEW'){
            this.body='您点击了菜单中的链接：'+message.EventKey
       }else if(message.Event==='scancode_push'){
            this.body='您点击了菜单中'+message.EventKey
        }else if(message.Event==='scancode_waitmsg'){
            this.body='您点击了菜单中扫码推送'+message.EventKey
       }else if(message.Event==='pic_sysphoto'){
            this.body='您点击了菜单中'+message.EventKey
       }else if(message.Event==='pic_photo_or_album'){
            this.body='您点击了菜单中'+message.EventKey
       }
   }else if(message.MsgType==='text'){
       var content=message.Content
       var reply='额，您说的'+message.Content+'太复杂了'

       if (content==='1') {
           reply='杨幂'
       }
       if (content==='2') {
           reply='迪丽热巴'
        }
       if (content==='3') {
           reply='古力娜扎'
        }
       if (content==='4') {
           reply=[{
                  title:'杨幂惊魂一夜' ,
                  description:'杨幂演过的电影',
                  picUrl:'http://img1.xiazaizhijia.com/walls/20151208/mid_5e397c3b1b6b20d.jpg',
                  url:'https://baike.baidu.com/item/%E6%9D%A8%E5%B9%82/149851?fr=aladdin'
               },{
                    title:'迪丽热巴巴扎黑' ,
                    description:'克拉恋人',
                    picUrl:'http://img.mp.itc.cn/q_mini,c_zoom,w_640/upload/20170802/5e013f17f53647feb7a831a5dbd350ea_th.jpg',
                    url:'https://baike.baidu.com/item/%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4/1180418?fr=aladdin'
                }]
        }
        if (content==='5') {
             var data=yield wechatApi.uploadMaterial('image',path.join(__dirname,'../2.jpg'))
             reply={
                  type:'image',
                  mediaId:data.media_id
                  
             }
        }

        if (content==='6') {
               var data=yield wechatApi.uploadMaterial('video',path.join(__dirname,'../3.mp4') )
               reply={
                    type:'video',
                    mediaId:data.media_id,
                    title:'抖音',
                    description:'搞笑三国'
               }
        }

        if (content==='7') {
               var data=yield wechatApi.uploadMaterial('image', path.join(__dirname,'../2.jpg'))
               reply={
                    type:'music',
                    title:'爱的供养',
                    description:'杨幂',
                    musicUrl:'http://sc1.111ttt.cn:8282/2015/1/04m/07/97071805046.m4a?tflag=1546606800&pin=97bb2268ae26c20fe093fd5b0f04be80#.mp3',
                    thumbMediaId:data.media_id,
               }
          }

          if (content==='8') {
               var data=yield wechatApi.uploadMaterial('image',path.join(__dirname,'../2.jpg'),{type:'image'})
               reply={
                    type:'image',
                    mediaId:data.media_id
                    
               }
          }

          if (content==='9') {
               var data=yield wechatApi.uploadMaterial('video',path.join(__dirname,'../3.mp4'),{type:'video',description:'{"title":"永久视频","introduction":"Newer think it"}'})
               reply={
                    type:'video',
                    mediaId:data.media_id,
                    title:'抖音',
                    description:'搞笑三国'
               }
          }

          if (content==='10') {
               var picData=yield wechatApi.uploadMaterial('image',path.join(__dirname,'../2.jpg'),{})
               console.log('picData:',picData)
               var media={
                    articles:[{
                         title:'杨幂',
                         thumb_media_id:picData.media_id,
                         author:'东哥',
                         digest:'没有摘要',
                         show_cover_pic:1,
                         content:'没有内容',
                         content_source_url:'https://baike.baidu.com/item/%E6%9D%A8%E5%B9%82/149851?fr=aladdin'
                    }]
               }

               data=yield wechatApi.uploadMaterial('news',media,{})

               console.log('10:00',data)
               data=yield wechatApi.fetchMaterial(data.media_id,'news',{})

               console.log('10:01',data)

               var items=data.news_item
               var news=[]

               items.forEach(function(item){
                   news.push({
                        title:item.title,
                        description:item.digest,
                        picUrl:picData.url,
                        url:item.url
                   })
               })

               reply=news
          }
         
          if (content==='11') {
               var counts= yield wechatApi.countMaterial()
               console.log(JSON.stringify(counts))

               var list1=yield wechatApi.batchMaterial({
                    type:'image',
                    offset:0,
                    count:10
               })
               var list2=yield wechatApi.batchMaterial({
                    type:'video',
                    offset:0,
                    count:10
               })
               var list3=yield wechatApi.batchMaterial({
                    type:'voice',
                    offset:0,
                    count:10
               })
               var list4=yield wechatApi.batchMaterial({
                    type:'news',
                    offset:0,
                    count:10
               })
               var results=[list1,list2,list3,list4]
                console.log(JSON.stringify(results))  
                
                reply='1'
          }

          if (content==='12') {
               var tag=yield wechatApi.createTag('如皋')

               console.log('新分组 如皋')
               console.log(tag)

               var tags=yield wechatApi.fetchTag()

               console.log('加了如皋后的分组列表')
               console.log(tags)

               var movetags=yield wechatApi.batchupdate(message.FromUserName,'103')

               console.log('我移动到六团分组')
               console.log(movetags)

               var wheretags=yield wechatApi.checkTag(message.FromUserName)

               console.log('查看当前我所在分组')
               console.log(wheretags)

               var editortags=yield wechatApi.updatetag('101','wechat2改为了江安分组')

               console.log('修改编辑分组')
               console.log(editortags)

               var tags01=yield wechatApi.fetchTag()

               console.log('查看最新分组列表')
               console.log(tags01)

               var deltags=yield wechatApi.deletetag('100')

               console.log('删除100分组')
               console.log(deltags)

               var tags02=yield wechatApi.fetchTag()

               console.log('查看最新分组列表')
               console.log(tags02)

               reply='用户管理'
          }

          if (content==='13') {
               var user=yield wechatApi.fetchUsers(message.FromUserName)
               console.log(user)

               var openIds=[
                    {
                         openid:message.FromUserName,
                         lang:'zh_CN'
                    }
               ]
               var users=yield wechatApi.fetchUsers(openIds)
               console.log(users)

               reply=JSON.stringify(user)
          }

          if (content==='14') {
               var userlist=yield wechatApi.listkUsers()
               console.log(userlist)
               reply=userlist.total+'个粉丝'
          }

          if (content==='15') {
               var text={
                    content:'我是群发推送'
               }
               var msgData= yield wechatApi.sendByGroup('text',text,'103')
               console.log('msgData',msgData)
               reply='success'
          }

          if (content==='16') {
              var msgData=yield wechatApi.checkMass('1000000002')
               console.log('msgData',msgData)
               reply='success'
          }
          if (content==='17') {
               var msgData=yield wechatApi.deleteMenu()
               console.log('deleteMenu17',msgData)
               reply='177'
           }
          
        this.body=reply
   }

   yield next
}